//
//  LSPhoneResetVController.m
//  LookScore
//
//  Created by wr on 2017/12/16.
//  Copyright © 2017年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSPhoneResetVController.h"
#import "JKCountDownButton.h"
#import "LSResetPasswardVController.h"
#import "NSString+JKNormalRegex.h"
#import "LSHttpEnclosureTool.h"
#import "MBProgressHUD+MJ.h"
#import "LSAccountArchive.h"
@interface LSPhoneResetVController ()
@property (weak, nonatomic) IBOutlet UITextField *oldPhoneTF;
@property (copy, nonatomic) NSString *oldPhone;
@property (weak, nonatomic) IBOutlet UITextField *reSetPhoneTF;
@property (weak, nonatomic) IBOutlet UITextField *verificationTF;
@property (weak, nonatomic) IBOutlet JKCountDownButton *verificationBu;
@property (weak, nonatomic) IBOutlet UIButton *sureBu;
@end

@implementation LSPhoneResetVController
//{
//    NSString *_verificationCode;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.title=@"修改手机号";
    self.view.backgroundColor=[UIColor whiteColor];
    [self.verificationBu setBackgroundImage:[CommonClass getImageWithBgColor:LSSubColor7ec5fd] forState:UIControlStateNormal];
    [self.verificationBu setBackgroundImage:[CommonClass getImageWithBgColor:LSNavMainColor] forState:UIControlStateHighlighted];
    
    [self.sureBu setBackgroundImage:[CommonClass getImageWithBgColor:LSSubColor7ec5fd] forState:UIControlStateNormal];
    [self.sureBu setBackgroundImage:[CommonClass getImageWithBgColor:LSNavMainColor] forState:UIControlStateHighlighted];
    
    
    [self.verificationBu countDownButtonHandler:^(JKCountDownButton*sender, NSInteger tag) {
        sender.enabled = NO;
        if (![self.reSetPhoneTF.text jk_isMobileNumber]) {
            [MBProgressHUD showOnlyMessage:@"请输入有效手机号" afterDelay:1.5f];
            sender.enabled = YES;
        }else{
            
            [sender startCountDownWithSecond:60];
            
            [sender countDownChanging:^NSString *(JKCountDownButton *countDownButton,NSUInteger second) {
                NSString *title = [NSString stringWithFormat:@"剩余%zds",second];
                return title;
            }];
            [sender countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
                countDownButton.enabled = YES;
                return @"重新获取";
                
            }];
            NSDictionary *validateDic=@{@"mobile":self.reSetPhoneTF.text};
            [LSHttpEnclosureTool POST:LSUserFindPwdValidate parameters:validateDic resBlock:^(id responseObject) {
                if (responseObject) {
                    
                }
            }];
        }
        
    }];
    
}
- (IBAction)verticationEvent:(JKCountDownButton *)sender {
    [LSHttpEnclosureTool POST:LSUserOtherValidateMobile parameters:@{@"mobile":self.reSetPhoneTF.text} resBlock:^(id responseObject) {
        if (responseObject) {
//            MyLog(@"修改手机号 验证码 --- %@",responseObject);
//            _verificationCode = responseObject;
        }
    }];
}
- (IBAction)sureResetEvent:(UIButton *)sender {
    if (![self.oldPhoneTF.text jk_isMobileNumber]) {
        [MBProgressHUD showOnlyMessage:@"请输入有效旧手机号" afterDelay:1.5f];
    }else if (![self.reSetPhoneTF.text jk_isMobileNumber]) {
        [MBProgressHUD showOnlyMessage:@"请输入有效新手机号" afterDelay:1.5f];
    }
    else if (self.verificationTF.text.length<4||self.verificationTF.text.length>6){
        [MBProgressHUD showOnlyMessage:@"请输入正确格式的验证码" afterDelay:1.5f];
    }else if (![self.oldPhoneTF.text isEqualToString:[LSAccountArchive account].Mobile]){
        [MBProgressHUD showOnlyMessage:@"请输入正确旧手机号" afterDelay:1.5f];
    }
    else{
        NSDictionary *changeMobileDic=@{@"cid":[LSAccountArchive account].Cid,@"validateCode":self.verificationTF.text,@"oldmobile":self.oldPhoneTF.text,@"mobile":self.reSetPhoneTF.text};
        [LSHttpEnclosureTool POST:LSUserChangeMobile parameters:changeMobileDic resBlock:^(id responseObject) {
            if (responseObject) {
                LSAccountCoder *coderModel = [LSAccountArchive account];
                coderModel.Mobile=responseObject;
                coderModel.ModifyMobileCount = 0;
                [LSAccountArchive saveAccount:coderModel];
//                [LSAccountArchive account].Mobile = responseObject;
//                [LSAccountArchive account].ModifyMobileCount = 0;
//                LSLoginVController *login = [[LSLoginVController alloc] init];
                if (self.PersonalDataChangedBlock) {
                    self.PersonalDataChangedBlock();
                }
                [self.navigationController popViewControllerAnimated:YES];
//                [self.navigationController presentRootViewController:login animated:YES completion:nil];
            }
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
